package com.example.myproject;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.myproject.fragment.GifFragment;
import com.example.myproject.fragment.HomeFragment;
import com.example.myproject.fragment.InformationFragment;
import com.example.myproject.fragment.MineFragment;
import com.example.myproject.fragment.ShoppingCartFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * create by xzk 2018/03/23
 * 商城首页
 */
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    private FragmentManager fm;
    private List<Fragment> fragList;
    private List<ImageView> imagTableList;

    private int targetCheck = 0;//当前选中frag的脚标

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        imagTableList = new ArrayList<>(5);
        ImageView tab_imag1 = findViewById(R.id.tab_imag1);
        tab_imag1.setSelected(true);
        imagTableList.add(tab_imag1);
        ImageView tab_imag2 = findViewById(R.id.tab_imag2);
        imagTableList.add(tab_imag2);
        ImageView tab_imag3 = findViewById(R.id.tab_imag3);
        imagTableList.add(tab_imag3);
        ImageView tab_imag4 = findViewById(R.id.tab_imag4);
        imagTableList.add(tab_imag4);
        ImageView tab_imag5 = findViewById(R.id.tab_imag5);
        imagTableList.add(tab_imag5);
        for (int i = 0; i < imagTableList.size(); i++) {
            imagTableList.get(i).setOnClickListener(this);
        }

        initFragment();
    }

    private void initFragment() {
        fragList = new ArrayList<>(5);
        HomeFragment homeFrag = new HomeFragment();
        GifFragment gifFrag = new GifFragment();
        ShoppingCartFragment shpFrag = new ShoppingCartFragment();
        InformationFragment infFrag = new InformationFragment();//跳转扫一扫
        MineFragment mineFrag = new MineFragment();

        fragList.add(homeFrag);
        fragList.add(gifFrag);
        fragList.add(shpFrag);
        fragList.add(infFrag);
        fragList.add(mineFrag);
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.home_content, fragList.get(4));
        transaction.hide(fragList.get(4));
        transaction.add(R.id.home_content, fragList.get(3));
//        transaction.hide(fragList.get(3));
        transaction.add(R.id.home_content, fragList.get(2));
        transaction.hide(fragList.get(2));
        transaction.add(R.id.home_content, fragList.get(1));
        transaction.hide(fragList.get(1));
        transaction.add(R.id.home_content, fragList.get(0));
        transaction.hide(fragList.get(0));
        transaction.commit();
    }

    private void selFragment(int index) {
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.show(fragList.get(index));
        transaction.hide(fragList.get(targetCheck));
        this.targetCheck = index;
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab_imag1:
                setTabStatus(0);
                break;
            case R.id.tab_imag2:
                setTabStatus(1);
                break;
            case R.id.tab_imag3:
                setTabStatus(2);
                break;
            case R.id.tab_imag4:
                setTabStatus(3);
                break;
            case R.id.tab_imag5:
                setTabStatus(4);
                break;
            default:
                Log.e("tags", "未标记id");
                break;
        }
    }

    private void setTabStatus(int target) {
        imagTableList.get(targetCheck).setSelected(false);
        imagTableList.get(target).setSelected(true);
        selFragment(target);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (fragList.get(3) instanceof InformationFragment) {
            InformationFragment inf = (InformationFragment) fragList.get(3);
            inf.onActivityResult(requestCode, resultCode, data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
